import 'package:json_annotation/json_annotation.dart';

part 'user_total.g.dart';

@JsonSerializable()
class UserTotal {
  final int userId;
  final String invitor;
  final int storeId;
  final String storeName;
  final String storeAuditStatus; // NOT_APPLY, AUDITING, AUDIT_SUCCESS, AUDIT_FAIL
  final String storeAuditStatusText;
  final int imageFileMap; // 低位的3个比特位，从低到高 店铺LOGO、身份证照、营业执照，1为需要，0为不需要
  final List<TotalItem> totalItems;
  final Map<String, String> storeOptions;
  final List<int> unmanagedStoreIds;
  final String storeProtocolUrl;

  UserTotal(
    this.userId,
    this.invitor,
    this.storeId,
    this.storeName,
    this.storeAuditStatus,
    this.storeAuditStatusText,
    this.imageFileMap,
    this.totalItems,
    this.storeOptions,
    this.unmanagedStoreIds,
    this.storeProtocolUrl,
  );

  factory UserTotal.fromJson(Map<String, dynamic> json) => _$UserTotalFromJson(json);

  Map<String, dynamic> toJson() => _$UserTotalToJson(this);

  int total(String totalType) {
    return totalItems
        .firstWhere(
          (item) => item.totalType == totalType,
          orElse: () => null,
        )
        ?.total;
  }

  String totalInfo(String totalType) {
    return totalItems
        .firstWhere(
          (item) => item.totalType == totalType,
          orElse: () => null,
        )
        ?.totalInfo;
  }
}

@JsonSerializable()
class TotalItem {
  final String totalType;
  final String totalTypeName;
  final int total;
  final String totalInfo;

  TotalItem(this.totalType, this.totalTypeName, this.total, this.totalInfo);

  factory TotalItem.fromJson(Map<String, dynamic> json) => _$TotalItemFromJson(json);

  Map<String, dynamic> toJson() => _$TotalItemToJson(this);
}
